<template>
  <el-popover :persistent="false" popper-class="searchPopover" placement="top-start" :width="255" trigger="click"
    :show-arrow="false">
    <template #reference>
      <section class="inputs">
        <div class="input">
          <img src="local-media://icons/search.png" alt="">
          <input type="text" placeholder="搜索用户昵称" v-model="searchText">
        </div>
      </section>
    </template>
    <section class="search_action">

      <template v-if="(result?.length > 0)">
        <ul class="searchInfo" v-for="first in result">
          <li class="title" v-if="(first.content?.length > 0)">{{ first?.title }}</li>
          <li class="item" v-for="row in first.content" @click="openDetail($event, row)">
            <el-avatar style="flex-shrink: 0;" :size="46" :src="row?.portrait" />
            <div>
              <span>{{ row?.name || row?.groupName || row?.nickName }}</span>
              <span>{{ row?.remark }}</span>
            </div>
            <img src="local-media://person.png" alt="" v-if="row?.nickName" class="person_img" />
          </li>
        </ul>
      </template>
      <div class="search_all" @click="searchAll">
        <img src="local-media://all.png" alt="" />
        <span>全局搜索</span>
        <img src="local-media://arraw.png" alt="" />
      </div>

      <el-popover width="270" v-model:visible="visible" placement="right-start" trigger="focus" virtual-triggering
        :virtual-ref="triggerRef" :teleported="false" :hide-after="0" :show-arrow="false">
        <div>
          <FriendDetail @submit="(visible = false)" v-if="selectCardInfo?.userId" :userId="selectCardInfo?.userId" sm />
          <GroupDetail @submit="(visible = false)" sm :groupId="selectCardInfo?.groupId"
            v-if="selectCardInfo?.groupId" />
        </div>
      </el-popover>
    </section>
  </el-popover>
</template>
<script>
import { defineComponent, onMounted, computed, ref } from 'vue';
import { useStore } from 'vuex';
import FriendDetail from '@/views/Friend/FriendPanner/FriendDetail'
import GroupDetail from '@/views/Friend/FriendPanner/GroupDetail'
export default defineComponent({
  components: {
    FriendDetail,
    GroupDetail
  },
  setup (props, { emit }) {
    const store = useStore()
    const searchText = ref()
    const userInfo = computed(() => store.state.userInfo)
    const friend = computed(() => store.state.activePanners[3].data?.filter(r => r.userId !== userInfo.value.userId) || [])
    const group = computed(() => store.state.activePanners[0].data || [])
    const searchAll = () => {
      store.commit("saveSearchType", 1)
      store.commit('saveSearchSelectItem', [])
      store.commit('saveDialogState', true)
    }
    const result = computed(() => {
      const search = (data) => {
        return data?.filter(v => {
          const text = v.nickName || v.name
          return text.includes(searchText.value)
        })
      }
      if (searchText.value) {
        return [
          {
            title: "联系人",
            content: search(friend.value)
          },
          {
            title: "我的群聊",
            content: search(group.value)
          }
        ]
      }

      return []
    })
    onMounted(() => {
      store.dispatch('getFriendByType', { index: 0 })
      store.dispatch('getFriendByType', { index: 3 })
    })
    const visible = ref(false)
    const selectCardInfo = ref()
    const triggerRef = computed(() => ({
      getBoundingClientRect: () => position.value
    }))
    const position = ref({
      top: 0,
      left: 0,
      bottom: 0,
      right: 0
    })
    const openDetail = (e, row) => {
      setTimeout(() => {
        position.value = DOMRect.fromRect({
          width: 0,
          height: 0,
          x: e.clientX,
          y: e.clientY,
        })
        selectCardInfo.value = row
        visible.value = true
      }, 100);
      e.preventDefault()
      return
    }
    return {
      searchAll,
      result,
      searchText,
      triggerRef,
      selectCardInfo,
      visible,
      openDetail
    }
  }
})
</script>

<style scoped lang="less">
.inputs {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 36px;
  width: calc(var(--contentLeftWidth) - 40px);
  margin: 0 auto;

  &:focus {
    border: 1px solid #01E0EB;
  }

  .input {
    width: 100%;
    display: flex;
    align-items: center;
    border: 1px solid #EEEEEE;
    border-radius: 18px;
    overflow: hidden;
    background: #F7F7F7;
    height: 100%;

    input {
      border: none;
      outline: none;
      height: 100%;
      background: #F7F7F7;
      margin-left: 5px;
      flex: 1;
      padding-right: 7px;
    }

    img {
      width: 26px;
      height: 26px;
      object-fit: cover;
      margin-left: 7px;
      flex-shrink: 0;
    }
  }

  // .add {
  //   height: 100%;
  //   background: #F7F7F7;
  //   border-radius: 100px 100px 100px 100px;
  //   width: 36px;
  //   margin-left: 12px;
  //   display: flex;
  //   align-items: center;
  //   justify-content: center;
  //   cursor: pointer;
  // }
}

.search_action {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-height: 60vh;
  overflow-y: auto;

  &::-webkit-scrollbar {
    display: none;
  }

  .search_all {
    display: flex;
    align-items: center;
    padding: 8px 15px;

    >span {
      font-size: 14px;

      font-weight: 500;
      color: #242424;
      margin-left: 8px;
    }

    >img {
      &:first-child {
        width: 18px;
      }

      &:last-child {
        margin-left: auto;
      }
    }
  }

  .title {
    padding: 10px 12px;
    position: sticky;
    top: 0;
    background-color: #fff;
    font-size: 14px;

    font-weight: 400;
    color: #787878;
  }

  .item {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    cursor: pointer;

    &:hover {
      background: #F7F7F7;

      .person_img {
        display: inline-block;
      }
    }

    >div {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding: 5px 0;
      margin-left: 10px;
      height: 46px;

      >span {
        font-size: 14px;

        font-weight: 400;
        color: rgba(36, 36, 36, 1);
      }
    }

    .person_img {
      width: 22px;
      display: none;
      height: 22px;
      margin-left: auto;
      align-self: flex-start;
    }
  }
}

.searchInfo {
  display: flex;
  flex-direction: column;


}
</style>
<style>
.searchPopover {
  padding: 10px 0 !important;
  border-radius: 8px !important;
}
</style>
